package de.axone.wash.configuration;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;

import de.axone.wash.FileWash;
import de.axone.wash.Wash;
import de.axone.wash.Wash.CodingException;
import de.axone.wash.Wash.NotFoundException;
import de.axone.wash.Wash.WrongTypeException;
import de.axone.wash.handler.Handler;


public class Configuration {
	
	private FileWash properties;
	
	public Configuration( String pathToConfig ) throws ServletException {
		
		// Get Handler from propertie file
		File f = new File( pathToConfig );
		
		try {
			properties = new FileWash( f );
			
			properties.read();
			
		} catch( IOException e ) {
			throw new ServletException( e );
		} catch( CodingException e ) {
			throw new ServletException( e );
		}
		
		String handlerName;
		try {
			handlerName = properties.getString( "handler" );
			
		} catch( NotFoundException e1 ) {
			throw new ServletException( e1 );
		} catch( WrongTypeException e1 ) {
			throw new ServletException( e1 );
		}
	
		try {
			handler = (Handler)Class.forName( handlerName ).newInstance();
			handler.init( this );
			
		} catch( Exception e ) {
			throw new ServletException( e );
		}
	}
	
	private Handler handler;
	public Handler getHandler() {
		
		return handler;
	}
	
	public Wash getWash() {
		
		return properties;
	}
}
